package know.how.glutenfree;

import java.util.ArrayList;
import java.util.List;

import com.bappi.custom.activities.CustomActivity;
import com.bappi.custom.activities.CustomActivityGroup;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;

public class Tab2Activity1 extends CustomActivity {

	private ListView listView;
	private List<RestaurantItem> filteredListItems;
	private List<RestaurantItem> favoritesItems;
	private EfficientAdapter adapter;
	private EditText searchView;
	
	private final OnSharedPreferenceChangeListener ospcl = new OnSharedPreferenceChangeListener() {

		public void onSharedPreferenceChanged(final SharedPreferences sp,
				String key) {
			try {
				if(TabsController.KEY_FAVORITES_UPDATE_TIME.equals(key)){
					runOnUiThread(new Runnable() {
						public void run() {
							try {
								favoritesItems = TabsController.getFavoriteRestaurents();
								showFiltering();
							} catch (Exception e) {
								e.printStackTrace();
							}
						}
					});
				} else if(TabsController.KEY_TAB_1_CLICKED.equals(key)){
					runOnUiThread(new Runnable() {
						public void run() {
							try {
								((CustomActivityGroup)getParent()).backTo(1);
							} catch (Exception e) {
								e.printStackTrace();
							}
						}
					});
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	};

	@Override
	public void onCreate(Bundle icicle) {
		try {
			super.onCreate(icicle);
			setContentView(R.layout.layout_tab_2_screen_1);
			searchView = (EditText) findViewById(R.id.editText1);
			filteredListItems = new ArrayList<RestaurantItem>();
			listView = (ListView) findViewById(R.id.list_view);
			listView.setDividerHeight(0);
			adapter = new EfficientAdapter(this);
			listView.setAdapter(adapter);
			favoritesItems = TabsController.getFavoriteRestaurents();
			showFiltering();
			
			searchView.addTextChangedListener(new TextWatcher() {

				public void onTextChanged(CharSequence s, int start,
						int before, int count) {
					// TODO Auto-generated method stub
					showFiltering();
				}

				public void beforeTextChanged(CharSequence s, int start,
						int count, int after) {
					// TODO Auto-generated method stub

				}

				public void afterTextChanged(Editable s) {
					// TODO Auto-generated method stub

				}
			});
			TabsController.getSharedPreferences().registerOnSharedPreferenceChangeListener(ospcl);

		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	
	@Override
	protected void onDestroy() {
		try {
			TabsController.getSharedPreferences().unregisterOnSharedPreferenceChangeListener(ospcl);
		} catch (Exception e) {
			// TODO: handle exception
		}
		super.onDestroy();
	}
	
	private void showFiltering(){
		try {
			String key = searchView.getText().toString().toLowerCase();
			filteredListItems.clear();
			if(key.length()>0){
				RestaurantItem listItem;
				for (int i = 0; i < favoritesItems.size(); i++) {
					listItem = favoritesItems.get(i);
					if (listItem.getTitle().toLowerCase().contains(key)) {
						filteredListItems.add(listItem);
					}
				}
			}else{
				filteredListItems.addAll(favoritesItems);
			}
			adapter.notifyDataSetChanged();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public class EfficientAdapter extends BaseAdapter {
		private LayoutInflater mInflater;

		public EfficientAdapter(Context context) {
			mInflater = LayoutInflater.from(context);
		}

		public View getView(final int position, View convertView,
				ViewGroup parent) {
			try {
				ViewHolder holder;
				if (convertView == null) {
					convertView = mInflater
							.inflate(R.layout.restaurant_list_content, null);
					holder = new ViewHolder();
					holder.mainContainer = convertView
							.findViewById(R.id.main_container);
					holder.button = (Button) convertView
							.findViewById(R.id.button);
					convertView.setTag(holder);
				} else {
					holder = (ViewHolder) convertView.getTag();
				}
				Drawable drawable = getResources().getDrawable(
						getResources().getIdentifier(
								filteredListItems.get(position)
										.getDrawableName(), "drawable",
								getClass().getPackage().getName()));
				holder.button.setCompoundDrawablesWithIntrinsicBounds(drawable,
						null, null, null);
				holder.button.setText(filteredListItems.get(position)
						.getTitle());
				holder.button.setOnClickListener(new View.OnClickListener() {

					public void onClick(View v) {
						final RestaurantItem listItem = filteredListItems
								.get(position);

						Intent intent = new Intent(getParent(),
								RestaurantDetailsActivity.class);
						intent.putExtra(RestaurantDetailsActivity.KEY_ID,
								listItem.getId());
						pushIntnet(intent);
					}
				});
			} catch (Exception e) {
				e.printStackTrace();
			}
			return convertView;
		}

		class ViewHolder {
			View mainContainer;
			Button button;
		}

		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return 0;
		}

		public int getCount() {
			return filteredListItems.size();
		}

		public Object getItem(int position) {
			return filteredListItems.get(position);
		}
	}
}